package main

import "fmt"

// 抽象组件接口（节点）
type FileSystemComponent interface {
	Name() string
	Size() int             // 获取文件/文件夹大小（单位：MB）
	Display(indent string) // 递归显示结构
}

// ---------------------- 叶子节点：文件 ----------------------
type File struct {
	name string
	size int
}

func NewFile(name string, size int) *File {
	return &File{name: name, size: size}
}

func (f *File) Name() string {
	return f.name
}

func (f *File) Size() int {
	return f.size
}

func (f *File) Display(indent string) {
	fmt.Printf("%s📄 %s (%dMB)\n", indent, f.name, f.size)
}

// ---------------------- 组合节点：文件夹 ----------------------
type Folder struct {
	name     string
	children []FileSystemComponent
}

func NewFolder(name string) *Folder {
	return &Folder{
		name:     name,
		children: make([]FileSystemComponent, 0),
	}
}

func (d *Folder) Name() string {
	return d.name
}

func (d *Folder) Size() int {
	total := 0
	for _, child := range d.children {
		total += child.Size()
	}
	return total
}

func (d *Folder) Add(c FileSystemComponent) {
	d.children = append(d.children, c)
}

func (d *Folder) Remove(targetName string) bool {
	for i, child := range d.children {
		if child.Name() == targetName {
			d.children = append(d.children[:i], d.children[i+1:]...)
			return true
		}
	}
	return false
}

func (d *Folder) Display(indent string) {
	fmt.Printf("%s📁 %s (总大小: %dMB)\n", indent, d.name, d.Size())
	for _, child := range d.children {
		child.Display(indent + "    ")
	}
}

// ---------------------- 客户端调用 ----------------------
func main() {
	// 构建文件系统树
	root := NewFolder("根目录")

	docFolder := NewFolder("文档")
	docFolder.Add(NewFile("简历.pdf", 3))
	docFolder.Add(NewFile("合同.docx", 5))

	mediaFolder := NewFolder("媒体")
	mediaFolder.Add(NewFile("婚礼视频.mp4", 1024))
	mediaFolder.Add(NewFile("合影.jpg", 8))

	root.Add(docFolder)
	root.Add(mediaFolder)
	root.Add(NewFile("README.txt", 1))

	// 显示整个文件系统结构
	root.Display("")

	// 计算总大小
	fmt.Printf("\n总占用空间: %dMB", root.Size())
}
